package org.example.curriculum_design_6_16.controller;

import lombok.extern.slf4j.Slf4j;
import org.example.curriculum_design_6_16.entity.pojo.PoliticalPerformance;
import org.example.curriculum_design_6_16.entity.vo.Result;
import org.example.curriculum_design_6_16.service.PoliticalPerformanceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/political")
public class PoliticalController {
    @Autowired
    private PoliticalPerformanceService politicalPerformanceService;

    @GetMapping("/student/{studentId}")
    public Result<PoliticalPerformance> getByStudentId(@PathVariable String studentId, @RequestParam String academicYear) {
        PoliticalPerformance result = politicalPerformanceService.selectByStudentId(studentId, academicYear);
        return result != null ? Result.success(result) : Result.fail(404, "记录不存在");
    }

    @GetMapping("/all")
    public Result<PoliticalPerformance> getAll() {
        PoliticalPerformance result = politicalPerformanceService.getAllPoliticalPerformance();
        return result != null ? Result.success(result) : Result.fail(404, "无记录");
    }

    @DeleteMapping("/student")
    public Result<Integer> deleteByStudentId(@RequestParam String studentId) {
        int result = politicalPerformanceService.deleteByStudentId(studentId);
        return result > 0 ? Result.success(result) : Result.fail(404, "删除失败");
    }

    @PutMapping("/score")
    public Result<Integer> updateScore(
            @RequestParam String studentId,
            @RequestParam double newScore) {
        int result = politicalPerformanceService.updateScore(studentId, newScore);
        return result > 0 ? Result.success(result) : Result.fail(404, "更新失败");
    }

    @GetMapping("/check-base-score")
    public Result<Boolean> checkBaseScore(@RequestParam String studentId) {
        boolean result = politicalPerformanceService.isBaseScoreZeroInSemester(studentId);
        return Result.success(result);
    }
}
